<?php
function stackoverflow_get_anonymous_user_id()
{
    $userId = username_exists('zhihu');
    if ($userId == null) {
        $userId = wp_insert_user(array(
            'user_login' => 'Stackoverflow',
            'user_email' => 'Stackoverflow@hengha.info',
            'nickname' => 'Stackoverflow',
            'display_name' => 'Stackoverflow',
            'user_pass' => wp_generate_password(20),
            'role' => 'subscriber'
        ));
    }
    return $userId;
}

function stackoverflow_get_user_id($user)
{
    if ($user && $user->name != '匿名') {
        $id = str_replace('http://stackoverflow.com/users/', '', $user->uri);
        $login = $id . '@stackoverflow.com';
        $userId = username_exists($login);
        if ($userId == null) {
            $userId = wp_insert_user(array(
                'nickname' => $user->name,
                'display_name' => $user->name,
                'user_login' => $login,
                'user_email' => $login,
                'user_pass' => wp_generate_password(20),
                'role' => 'subscriber'
            ));
        }
        return $userId;
    } else {
        return stackoverflow_get_anonymous_user_id();
    }
}
